package jok.net;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.net.Socket;

public class TCPClient {

	public static void main(String[] args) {
		Socket s = null;
		BufferedWriter bw = null;
		BufferedReader br = null;
		try {
			// 申明客户端Socket并连接服务端
			s = new Socket("192.168.1.20", 10001);
			// 持续发送
			while (true) {
				// 读取控制台输入
				br = new BufferedReader(new InputStreamReader(System.in));
				String line = null;
				if ((line = br.readLine()) != null) {
					// 获得输出字节流
					OutputStream os = s.getOutputStream();
					// 输出字符流转换为字节流用于输入
					bw = new BufferedWriter(new OutputStreamWriter(os));
					bw.write(line);
					bw.newLine();
					bw.flush();
					// 定义退出
					if ("exit".equals(line)) {
						break;
					}
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			try {
				if (br != null)
					br.close();
				if (s != null)
					s.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}
